package com.lifeng.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.apache.ibatis.reflection.MetaObject;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;

/**
 * @author: lifeng
 * @description:MybatisPlus的配置类（方便后续使用mybatisplus的其它功能）
 *
 **/
@Configuration
@MapperScan("com.lifeng.mapper")
public class MybatisPlusConfig implements MetaObjectHandler {


	// 配置分页拦截器插件
	//@Bean受容器管理
	@Bean
	public MybatisPlusInterceptor plusInterceptor() {
		MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
		mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
		return mybatisPlusInterceptor;
	}

	// 插入操作自动填充时间
	@Override
	public void insertFill(MetaObject metaObject) {
		this.setFieldValByName("createTime", LocalDateTime.now(),metaObject);
		this.setFieldValByName("updateTime", LocalDateTime.now(),metaObject);
	}

	//更新操作自动填充时间
	@Override
	public void updateFill(MetaObject metaObject) {
		this.setFieldValByName("updateTime", LocalDateTime.now(),metaObject);
	}
}
